package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.util.TimeUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.base.BaseGWebViewActivity;
import com.ysxsoft.wdg.bean.response.DaiKaiDetailResponse;
import com.ysxsoft.wdg.bean.response.DaiKaiResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.WebViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

public class MyDaiKaiMenuWebViewActivity extends BaseActivity {
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    public static void start(Context context, String title, String id) {
        Intent i = new Intent(context, MyDaiKaiMenuWebViewActivity.class);
        i.putExtra("title", title);
        i.putExtra("id", id);
        context.startActivity(i);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dai_kai_web_view;
    }

    @Override
    protected void initData() {
        String t = getIntent().getStringExtra("title");
        String id = getIntent().getStringExtra("id");
        initStatusBar(topView, false);
        titleContent.setText(t);
        WebViewUtils.init(webView);
//        WebViewUtils.setData(webView, c);
        getDetail(id);
    }

    private void getDetail(String id) {
        OkHttpUtils.post()
                .url(UrlHost.MY_DAI_DETAIL)
                .addParams("did", id)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DaiKaiDetailResponse daiKaiResponse = JsonUtils.parseByGson(response, DaiKaiDetailResponse.class);
                        if (daiKaiResponse != null) {
                            if (CallbackCode.SUCCESS.equals(daiKaiResponse.getCode())) {//请求成功
                                DaiKaiDetailResponse.DataBean data = daiKaiResponse.getData();
                                time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(data.getAddtime()*1000)).toString());
                                title.setText(data.getTitle());
                                if (data != null) {
                                    WebViewUtils.setData(webView, data.getContent());
                                } else {
                                }
                            } else {
//                                ToastUtils.showToast("获取个人中心菜单出错!");
                            }
                        }
                    }
                });
    }

    @Override
    protected void setListener() {
    }

    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

}
